package com.my.gesturelockandwakedemo.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
import android.widget.Toast;

import com.my.gesturelockandwakedemo.R;
import com.my.gesturelockandwakedemo.util.CacheActivity;
import com.my.gesturelockandwakedemo.util.SharedPrefUtil;
import com.my.gesturelockandwakedemo.view.GestureLockDisplayView;
import com.my.gesturelockandwakedemo.view.GestureLockLayout;
import com.my.gesturelockandwakedemo.view.JDLockView;

import java.util.List;

public class SettingGestureLockActivity extends BaseActivity {
    /*** 手势解锁 */
    private GestureLockLayout mGestureLockLayout;
    /*** 手势解锁提示 */
    private GestureLockDisplayView mGestureLockDisplayView;
    /*** 提示Text */
    private TextView tvHint;
    // hndler
    private Handler mHandler = new Handler();
    /****/
    private SharedPrefUtil sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_gesture_lock);
        sp = new SharedPrefUtil(this, "MyGestureLock");

        initViews();
        initOnClickEvents();
    }

    @Override
    public void initViews() {
        super.initViews();
        tvHint = (TextView) findViewById(R.id.tv_setting_hint);

        mGestureLockLayout = (GestureLockLayout) findViewById(R.id.gl_view);
        mGestureLockDisplayView = (GestureLockDisplayView) findViewById(R.id.gd_view);
        // 设置提示view 每行每列点的个数
        mGestureLockDisplayView.setDotCount(3);
        // 设置提示View 的原色
        mGestureLockDisplayView.setDotSelectedColor(Color.parseColor("#01A0E5"));
        // 设置提示View 的非选中颜色
        mGestureLockDisplayView.setDotUnSelectedColor(Color.TRANSPARENT);
        // 设置手势解锁View 每行没列的点数
        mGestureLockLayout.setDotCount(3);
        // 设置手势锁View 的最少连接数
        mGestureLockLayout.setMinCount(4);
        // 设置解锁模式 默认为JD
        mGestureLockLayout.setLockView(new JDLockView(this));
    }

    @Override
    public void initOnClickEvents() {
        super.initOnClickEvents();

        mGestureLockLayout.setOnLockResetListener(new GestureLockLayout.OnLockResetListener() {
            @Override
            public void onConnectCountUnmatched(int connectCount, int minCount) {
                // 连接数小于最小连接数时调用
                tvHint.setText("最少连接" + minCount + "个点");
                // 重置
                resetGesture();
            }

            @Override
            public void onFirstPasswordFinished(List<Integer> answerList) {
                // 第一次绘制成功时调用
                tvHint.setText("确认解锁图案");
                // 将答案设置给提示View
                mGestureLockDisplayView.setAnswer(answerList);
                // 重置
                resetGesture();
            }

            @Override
            public void onSetPasswordFinished(boolean isMatched, List<Integer> answerList) {
                // 第二次绘制调用
                if (isMatched) { // 两次绘制一致
                    sp.putString("MyGesture", answerList.toString());// TODO
                    sp.putBoolean("GestureFlag", true);
                    sp.commit();
                    // Log.i("mwb", "answerList：" + answerList.toString());

                    Toast.makeText(SettingGestureLockActivity.this, "密码设置成功！", Toast.LENGTH_SHORT).show();
                    // 设置成功结束当前界面
                    CacheActivity.finishSingleActivity(SettingGestureLockActivity.this);
                    // 跳转到手势验证界面
                    goIntent(CheckGestureLockActivity.class);
                } else {
                    resetGesture();
                    Toast.makeText(SettingGestureLockActivity.this, "两次密码输入不一致！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 页面跳转
     */
    private void goIntent(Class cla) {
        Intent intent = new Intent(this, cla);
        startActivity(intent);
    }

    /**
     * 重置
     */
    private void resetGesture() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mGestureLockLayout.resetGesture();
            }
        }, 200);
    }

}
